#pragma once

#include <ring.h>
#include <list.h>
#include <queue.h>
#include <length.h>
#define KERNEL_WAITING_THREAD		32

struct MUTEX_LOCKER
{
	void* lnow;
	slnode* waiting;
	_ptlong state;
//	inline struct MUTEX_LOCKER& operator=(struct MUTEX_LOCKER& source);
};

struct KERNEL_MUTEX_LOCKER
{
	void* lnow;
	queue waiting;
};

unsigned int* KERNEL_WAITING[KERNEL_WAITING_THREAD];

typedef struct MUTEX_LOCKER	_mlock;
typedef struct KERNEL_MUTEX_LOCKER _klock;

extern volatile ring* run;


#define SPIN_NORMAL_LOCK(x)	\
while(!(x)){}

#define SPIN_SCHEDU_LOCK(x)	\
while(!(x)){ThreadTurning();}

#define MUTEX_Owner(x)		(((_mlock*)x)->lnow)

inline _mlock* MUTEX_Init(_mlock* locker);
inline ring* MUTEX_MustLock(_mlock* locker,ring* owner);
int KERNEL_MUTEX_Lock(_klock* locker,ring* owner);
int KERNEL_MUTEX_UnLock(_klock* locker,ring* owner);
void MUTEX_Lock(_mlock* locker,ring* owner);
int MUTEX_TryToLock(_mlock* locker,ring* owner);
int MUTEX_UnLock(_mlock* locker,ring* owner);
inline ring* MUTEX_MustLock(_mlock* locker,ring* owner);
#define MUTEX_LOCK(x)    MUTEX_Lock((_mlock*)x,(ring*)run)
#define MUTEX_TRYLOCK(x) MUTEX_TryToLock((_mlock*)x,(ring*)run)
#define MUTEX_UNLOCK(x)	 MUTEX_UnLock((_mlock*)x,(ring*)run)
#define MUTEX_SPLOCK(x)  MUTEX_SpecailLock((_mlock*)x,(ring*)run)


#define IN_NEED  (1 << 1)
//IN_HURRY means this locker is in need of by the user of Special_locker.When it's unlocked,it will always be Scheduled until it's clear the
//NEED_LOCKER bit. 
